package com.clstu.practice;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 这是注册窗口的界面
 */
public class RegisterFrame extends JFrame implements ActionListener {

    private JLabel idLb = new JLabel("请输入账号");//组件成员
    private JTextField id = new JTextField(10);
    private JLabel pwdLb = new JLabel("请输入密码");
    private JPasswordField pwd = new JPasswordField(10);
    private JLabel pwdLb2 = new JLabel("请确认密码");
    private JPasswordField pwd2 = new JPasswordField(10);
    private JLabel nameLb = new JLabel("请输入姓名");
    private JTextField name = new JTextField(10);
    private JLabel sexLb = new JLabel("请选择你的性别");
    private JRadioButton sex1 = new JRadioButton("男",true);
    private JRadioButton sex2 = new JRadioButton("女",false);
    private ButtonGroup bgSex = new ButtonGroup();
    private JButton btRegis = new JButton("注册");
    private JButton btExit = new JButton("退出");

    public static void main(String[] args) {
        new RegisterFrame();
    }

    public RegisterFrame(){
        super("注册");
        this.setLayout(new FlowLayout());//流式布局

        add(idLb);//加入组件
        add(id);
        add(pwdLb);
        add(pwd);
        add(pwdLb2);
        add(pwd2);
        add(nameLb);
        add(name);
        bgSex.add(sex1);
        bgSex.add(sex2);
        add(sexLb);
        add(sex1);
        add(sex2);
        add(btRegis);
        add(btExit);

        setLocation(700,400);//设置窗口
        setSize(260,300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);

        btExit.addActionListener(this);//加入监听器
        btRegis.addActionListener(this);

    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if(e.getSource()==btRegis){
            String pwd = new String(this.pwd.getPassword());
            String pwd2 = new String(this.pwd2.getPassword());

            if(!pwd.equals(pwd2)){
                JOptionPane.showMessageDialog(this,"这两个密码不相同");
                return;
            }

            String id = this.id.getText();
            String name = this.name.getText();
            String sex;
            if(sex1.isSelected()){
                sex = (String) sex1.getText();
            }else {
                sex = (String) sex2.getText();
            }
            User.id = id;
            User.pwd = pwd;
            User.name = name;
            JOptionPane.showMessageDialog(this,id+"注册成功,"+","+name+","+sex);
            this.dispose();
            new LoginFrame();
        }else if(e.getSource() == btExit){
            this.dispose();
            new LoginFrame();
        }

    }
}
